package cn.itcast.oa.listener;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.itcast.oa.model.Privilege;
import cn.itcast.oa.service.PrivilegeService;
/**
 * 自定义监听器，实现servletCOntextListen接口
 * @author 留得残荷听雨声
 *
 */
public class InitServletContextListener implements ServletContextListener {

    @Override 
	public void contextInitialized(ServletContextEvent sce) {
    	
		ServletContext application = sce.getServletContext();

		// 得到Service的实例对象
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
		PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");
		
		// 准备所有顶级权限的集合（顶级菜单）
		List<Privilege> topPrivilegeList = privilegeService.findTopList();
		//放置topPrivilegeList到application上下文中
		application.setAttribute("topPrivilegeList", topPrivilegeList);
		
		System.out.println("-- 已准备好顶级权限的数据 --");
		
		List<String> allPrivilegeUrl=privilegeService.findAllPrivilegeUrl();
		application.setAttribute("allPrivilegeUrl", allPrivilegeUrl);
		
		System.out.println("-- 已准备好所有URL的数据 --");
	}
    
    @Override
	public void contextDestroyed(ServletContextEvent sce) {
	}

}
